package com.liao.furns.dao.impl;

import com.liao.furns.dao.BasicDAO;
import com.liao.furns.dao.FurnDAO;
import com.liao.furns.pojo.Furn;
import com.liao.furns.pojo.Page;
import javafx.beans.binding.NumberBinding;

import java.util.List;

/**
 * @author lzp
 * @version 1.0
 * lzpnb!
 */
public class FurnDAOImpl extends BasicDAO<Furn> implements FurnDAO {
    @Override
    public List<Furn> queryFurns() {
        String sql = "SELECT `id` , `name` , `maker` , `price` , `sales` , `stock` , `img_path` imgPath FROM `furn`";
        return queryMulti(sql,Furn.class);
    }

    @Override
    public int saveFurn(Furn furn) {
        String sql = "INSERT INTO furn(`id` , `name` , `maker` , `price` , `sales` , `stock` , `img_path`) \n" +
                "VALUES(NULL , ? , ? , ? , ? , " +
                "? , ?);";
        return update(sql,furn.getName(),furn.getMaker(),furn.getPrice(),furn.getSales(),furn.getStock(),furn.getimgPath());
    }

    @Override
    public int deleteFurn(Integer id) {
        String sql = "DELETE FROM `furn` WHERE id = ?";
        return update(sql,id);
    }

    @Override
    public Furn queryFurnById(int id) {
        String sql = "SELECT `id`,`name`,`maker`,`price`,`sales`,`stock`,`img_path` imgPath FROM `furn` WHERE id = ?";
        return querySingle(sql,Furn.class,id);
    }

    @Override
    public int updateFurn(Furn furn) {
        String sql = "UPDATE `furn` SET `name` = ?,`maker` = ?,\n" +
                "`price` = ?,`sales` = ?,`stock` = ?,\n" +
                "`img_path` = ? WHERE id = ?";
        return update(sql,furn.getName(),furn.getMaker(),furn.getPrice(),furn.getSales(),furn.getStock(),furn.getimgPath(),furn.getId());
    }

    @Override
    public int getTotalRow() {
        String sql = "SELECT COUNT(*) FROM `furn`";
        return ((Number)queryScalar(sql)).intValue();
    }

    @Override
    public int getTotalRowByName(String name) {
        String sql = "SELECT COUNT(*) FROM `furn` WHERE `name` LIKE ?";
        return ((Number)queryScalar(sql,"%" + name + "%")).intValue();
    }

    @Override
    public List<Furn> getPageItems(int begin, int pageSize) {
        String sql = "SELECT `id`,`name`,`maker`,`price`,`sales`,`stock`,`img_path` imgPath FROM `furn` LIMIT  ?,?";
        return queryMulti(sql, Furn.class,begin,pageSize);
    }

    @Override
    public List<Furn> getPageItemsByName(int begin, int pageSize, String name) {
        String sql = "SELECT `id`,`name`,`maker`,`price`,`sales`,`stock`,`img_path` imgPath FROM `furn` WHERE\n" +
                "`name` LIKE ? LIMIT  ?,?";
        return queryMulti(sql,Furn.class,"%" + name + "%",begin,pageSize);
    }
}
